package com.runjian.rundo.iot.agent.iec104.parser.frame.control.base;

import com.runjian.rundo.iot.agent.business.control.enums.ControlResult;
import com.runjian.rundo.iot.agent.iec104.consts.Cot;
import com.runjian.rundo.iot.agent.iec104.parser.frame.ASDUFrame;
import com.runjian.rundo.iot.agent.iec104.parser.frame.ControlFrame;
import com.runjian.rundo.iot.agent.iec104.parser.frame.PointRemoteControl;

import lombok.Getter;
import lombok.Setter;

/**
 * 基础控制侦
 *
 * @author CaoQingHua
 */
@Getter
@Setter
public abstract class BaseControlFrame extends ASDUFrame implements ControlFrame {

    /**
     * 是否下发回复
     */
    Boolean reply = Boolean.FALSE;

    /**
     * 控制结果
     */
    ControlResult controlResult = ControlResult.HANDLE;

    @Override
    public void init() {
        this.setCot(Cot.ACT);
    }

    @Override
    public ASDUFrame replyFrame() {
        if (this.getReply()) {
            return this;
        } else {
            return null;
        }
    }

    /**
     * 下一步行动项
     * 
     * @param cancel 是否终止
     */
    @Override
    public void next(Boolean cancel) {
        switch (this.getCot()) {
            case ACTCON:
                if(this.getSe()==1) {
                    //选择确认
                    if (cancel) {
                        //取消控制
                        this.setCot(Cot.DEACT);
                        this.setReply(Boolean.TRUE);
                        this.setSe((short)0);
                    } else {
                        //执行控制
                        this.setCot(Cot.ACT);
                        this.setReply(Boolean.TRUE);
                        this.setSe((short)0);
                        break;
                    }
                }else{
                    //控制执行中
                    this.controlResult = ControlResult.HANDLE;
                }
                break;
            case DEACTCON:
                //撤消认
                this.controlResult = ControlResult.FAILED;
                break;
            case ACTTERM:
                //控制结束
                this.controlResult = ControlResult.SUCCESS;
                break;
            default:
                break;
        }
    }
}
